package com.ruoyi.pcwl.controller;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pcwl.domain.CarouselItem;
import com.ruoyi.pcwl.domain.MediaFiles;
import com.ruoyi.pcwl.domain.dto.CarouselItemDto;
import com.ruoyi.pcwl.domain.query.CarouselItemQuery;

import com.ruoyi.pcwl.domain.vo.Result;
import com.ruoyi.pcwl.domain.enums.MediaTypeEnum;
import com.ruoyi.pcwl.minio.MinioUtil;
import com.ruoyi.pcwl.service.CarouselItemService;
import com.ruoyi.pcwl.service.IMediaFilesService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 轮播图信息 Controller
 */

@Api(tags = "轮播图子项控制层")
@RestController("carouselItemController")
@RequestMapping("/carouselItem")
public class CarouselItemController extends BaseController{

	@Resource
	private CarouselItemService carouselItemService;

	@Value("${minio.bucketName}")
	private String bucketName;

	@Autowired
	IMediaFilesService mediaFilesService;


	/**
	 * 根据条件分页查询
	 */
	@ApiOperation("根据条件分页查询")
	@GetMapping("/loadDataList")
	public Result loadDataList(CarouselItemQuery query){
//		System.out.println(query.getParams());
		return Result.ok(carouselItemService.selectPage(query));
	}

//	/**
//	 * 新增商铺环境信息
//	 *
//	 */
//	@ApiOperation("新增轮播图项")
//	@PostMapping("/addCarouselItem")
//	public Result addCarouselItem(CarouselItem carouselItem) {
////		System.out.println(carouselItem);
//
//		carouselItemService.add(carouselItem);
//
//		return Result.ok(null);
//	}

//	/**
//	 * 新增商铺环境信息
//	 *
//	 */
//	@ApiOperation("新增轮播图项")
//	@PostMapping("/addCarouselItem")
//	public Result addCarouselItem(CarouselItemDto carouselItemDto) {
//		MultipartFile[] files = carouselItemDto.getFiles();
//
//		for (MultipartFile file : files) {
//			//上传文件到minio
//			Map<String, Object> map = null;
//			try {
//				map = MinioUtil.uploadFile(file, MediaTypeEnum.CAROUSEL);
//			} catch (Exception e) {
//				throw new RuntimeException("上传文件失败");
//			}
//			//保存文件信息到数据库
////			MediaFiles mediaFiles = new MediaFiles();
////			mediaFiles.setFilename(file.getOriginalFilename());
////			mediaFiles.setFilePath(pic);
////			mediaFiles.setBucket(bucketName);
////			mediaFiles.setCategoryId(MediaTypeEnum.CAROUSEL.getCategoryId().longValue());
////			mediaFiles.setCategoryName(MediaTypeEnum.CAROUSEL.getCategoryName());
////			mediaFiles.setFileSize(file.getSize());
////			mediaFilesService.insertMediaFiles(mediaFiles);
//			//保存轮播图项信息
//			CarouselItem carouselItem = new CarouselItem();
//			BeanUtils.copyProperties(carouselItemDto,carouselItem);
//			carouselItem.setUrl(map.get("path").toString());
//			carouselItem.setPic(map.get("path").toString());
//			carouselItem.setMediaFileId(Long.parseLong(map.get("mediaFileId").toString()));
//			carouselItemService.add(carouselItem);
//		}
//
//		return Result.ok(null);
//	}

	/**
	 * 新增
	 */
	@ApiOperation("新增")
	@PostMapping("/add")
	public Result add(@RequestBody CarouselItem bean) {
		carouselItemService.add(bean);
		return Result.ok(null);
	}

	/**
	 * 批量新增
	 */
	@ApiOperation("批量新增")
	@PostMapping("/addBatch")
	public Result addBatch(@RequestBody List<CarouselItem> listBean) {
		carouselItemService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 批量新增/修改
	 */
	@ApiOperation("批量新增/修改")
	@PostMapping("/addOrUpdateBatch")
	public Result addOrUpdateBatch(@RequestBody List<CarouselItem> listBean) {
		carouselItemService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@ApiOperation("根据Id查询对象")
	@GetMapping("/getCarouselItemById/{id}")
	public Result getCarouselItemById(@PathVariable Long id) {
		return Result.ok(carouselItemService.getCarouselItemById(id));
	}

	/**
	 * 根据Id修改对象
	 */
	@ApiOperation("根据Id修改对象")
	@PutMapping("/updateCarouselItemById/{id}")
	public Result updateCarouselItemById(@RequestBody CarouselItem bean,@PathVariable Long id) {
		carouselItemService.updateCarouselItemById(bean);
		return Result.ok(null);
	}

	/**
	 * 根据Id删除
	 */

	@ApiOperation("根据Id删除")
	@DeleteMapping("/deleteCarouselItemById/{id}")
	public Result deleteCarouselItemById(@PathVariable Long[] id) {
		carouselItemService.deleteCarouselItemById(id);
		return Result.ok(null);
	}

	@ApiOperation("轮播数据导出")
//	@Log(title = "用户管理", businessType = BusinessType.EXPORT)
//	@PreAuthorize("@ss.hasPermi('system:user:export')")
	@PostMapping("/export")
	public void export(HttpServletResponse response, CarouselItem carouselItem)
	{
		List<CarouselItem> list = carouselItemService.list(new QueryWrapper<CarouselItem>().setEntity(carouselItem));
		ExcelUtil<CarouselItem> util = new ExcelUtil<CarouselItem>(CarouselItem.class);
		util.exportExcel(response, list, "轮播数据");
	}
}
